package it.univpm.diiga.memorysim.actions;

import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.util.Date;

import it.univpm.diiga.memorysim.data.DataPlugin;
import it.univpm.diiga.memorysim.data.Record;
import it.univpm.diiga.memorysim.kernel.shared.FileRequest;
import it.univpm.diiga.memorysim.kernel.shared.tree.File;
import it.univpm.diiga.memorysim.kernel.shared.tree.MovingBox;

import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;


public class Save implements IWorkbenchWindowActionDelegate {
	public static IWorkbenchWindow window;
	public static boolean codaprocessi=false;
	/**
	 * The constructor.
	 */
	public Save() {
	}


	public void run(IAction action) {
		String sim=DataPlugin.SimType+","+DataPlugin.SimSelected+","+DataPlugin.SimSelected1+","+DataPlugin.AlgSelected;
		String param=DataPlugin.memorycentraldata+","+DataPlugin.swapdata+","+DataPlugin.blockdata+","+DataPlugin.hdddimensionblockdata+","+DataPlugin.trasferratedata+","+DataPlugin.seekdata+","+DataPlugin.hdddimensiondata+","+DataPlugin.revolutiontimedata+","+DataPlugin.hddused+","+DataPlugin.ramUsed;
		String ListDimProcess=savePaginazione();
		String ListNumSeg=saveSegmentazione();
		String Procreq=saveProcessRequest();
		String InitRAM=saveRAMsituation();
		String InitSWAP=saveSWAPsituation();
		String root=saveFileSystem();
		Record rec=new Record(sim,param,ListDimProcess,ListNumSeg,Procreq,InitRAM,InitSWAP,root);
		//file request a parte!!!:
		setFileRequest(rec);
		Date d=new Date(System.currentTimeMillis());
		String nome= ""+d;
        FileOutputStream fos;
		try {
			fos = new FileOutputStream(Platform.getLocation()+"/"+nome.replace(":", "_")+".bin");
			ObjectOutputStream oos = new ObjectOutputStream(fos); 
            oos.writeObject(rec);
			oos.flush(); 		
			MessageDialog.openInformation(window.getShell(),"Salvataggio","Salvataggio eseguito con successo");
		} catch (Exception e) {MessageDialog.openInformation(window.getShell(),"Salvataggio","Errore Salvataggio: "+e.getMessage());} 
		
	}

	private void setFileRequest(Record rec) {
		String Name="", Dim ="", Path="",Operazione="";
		try{
			for(int i=0;i<DataPlugin.getFileListRequest().size()-1;i++){
		    	FileRequest dato = DataPlugin.getFileListRequest().get(i);
				Name+=dato.getName()+",";
				Dim+=dato.getDim()+",";
				Path+=dato.getPath()+",";
				Operazione+=dato.getTipoOperazione()+",";
			}
		//ultimo elemento
		FileRequest dato = DataPlugin.getFileListRequest().get(DataPlugin.getFileListRequest().size()-1);
		Name+=dato.getName();
		Dim+=dato.getDim();
		Path+=dato.getPath();
		Operazione+=dato.getTipoOperazione();
		}catch(Exception e){}
		rec.setFileRequest(Name,Dim, Path, Operazione);
	}


	private String saveSWAPsituation() {
		String SWAP = "";
		if(DataPlugin.SwapMemoryP!=null){
			for(int i=0;i<DataPlugin.SwapMemoryP.size()-1;i++){
				SWAP+=DataPlugin.SwapMemoryP.get(i)+",";
			}
			SWAP+=DataPlugin.SwapMemoryP.get(DataPlugin.SwapMemoryP.size()-1);
		}
		return SWAP;
	}


	private String saveRAMsituation() {
		String InitRAM = "";
		if(DataPlugin.CentralMemoryP!=null){
			for(int i=0;i<DataPlugin.CentralMemoryP.size()-1;i++){
				InitRAM+=DataPlugin.CentralMemoryP.get(i)+",";
			}
			InitRAM+=DataPlugin.CentralMemoryP.get(DataPlugin.CentralMemoryP.size()-1);
		}
		return InitRAM;
	}


	private String saveProcessRequest() {
		String Procreq="";
		if(DataPlugin.listProcessRequest!=null){
	    if(DataPlugin.listProcessRequest.size()>0){
		 for(int i=0;i<DataPlugin.listProcessRequest.size()-1;i++){
			 if(DataPlugin.listProcessRequest.get(i).getSegmento()==-1){
				 Procreq+=DataPlugin.listProcessRequest.get(i).getPID()+"-"+DataPlugin.listProcessRequest.get(i).getPagina()+"-"+DataPlugin.listProcessRequest.get(i).getType()+"-"+DataPlugin.listProcessRequest.get(i).getDimBlock()+",";
			 }else{
				 Procreq+=DataPlugin.listProcessRequest.get(i).getPID()+"-"+DataPlugin.listProcessRequest.get(i).getSegmento()+"-"+DataPlugin.listProcessRequest.get(i).getType()+"-"+DataPlugin.listProcessRequest.get(i).getDimBlock()+",";
			 }
		 }
		 int last=DataPlugin.listProcessRequest.size()-1;
		 if(DataPlugin.listProcessRequest.get(DataPlugin.listProcessRequest.size()-1).getSegmento()==-1){
			 Procreq+=DataPlugin.listProcessRequest.get(last).getPID()+"-"+DataPlugin.listProcessRequest.get(last).getPagina()+"-"+DataPlugin.listProcessRequest.get(last).getType()+"-"+DataPlugin.listProcessRequest.get(last).getDimBlock();
		 }else{
			 Procreq+=DataPlugin.listProcessRequest.get(last).getPID()+"-"+DataPlugin.listProcessRequest.get(last).getSegmento()+"-"+DataPlugin.listProcessRequest.get(last).getType()+"-"+DataPlugin.listProcessRequest.get(last).getDimBlock();
		 }
	    }}
		return Procreq;
	}


	private String saveSegmentazione() {
		String ListNumSeg="";
	    if(DataPlugin.listSegmentP.size()>0){
		 for(int i=0;i<DataPlugin.listSegmentP.size()-1;i++){
			ListNumSeg+=DataPlugin.listSegmentP.get(i)+",";
		 }
		 ListNumSeg+=DataPlugin.listSegmentP.get(DataPlugin.listSegmentP.size()-1)+"";
	    }
		return ListNumSeg;
	}


	private String savePaginazione() {
		String ListDimProcess="";
		if(DataPlugin.getlistProcess().size()>0){
		 for(int i=0;i<DataPlugin.getlistProcess().size()-1;i++){
		 	ListDimProcess+=DataPlugin.getlistProcess().get(i).getDimProcess()+",";
		 }
		 ListDimProcess+=DataPlugin.getlistProcess().get(DataPlugin.getlistProcess().size()-1).getDimProcess()+"";
		 }
		return ListDimProcess;
	}


	private String saveFileSystem() {
		String FileSystem = "";
		try{
		MovingBox disco=(MovingBox) DataPlugin.root.getBoxes().get(0);
		FileSystem=InTree(FileSystem,disco);
		FileSystem=FileSystem.substring(0,FileSystem.length()-1);
		}catch(Exception e){}
		return FileSystem;
	}
	
	private String InTree(String path,MovingBox disco) {
		String result;
		if(disco.getTitle()==null && disco.getName()!=null){
			
			result=path+"/"+disco.getName()+"\\."+((File) disco).dimensione+",";
		}else{
		path+="/"+disco.getTitle();
		result=path+",";
		for(int i=0;i<disco.getBoxes().size();i++){
			result+=InTree(path,(MovingBox) disco.getBoxes().get(i));
		}
		}
		return result;
	}

	
	

	public void selectionChanged(IAction action, ISelection selection) {
	}


	public void dispose() {
	}


	@SuppressWarnings("static-access")
	public void init(IWorkbenchWindow window) {
		this.window = window;
	}
}